package com.liu.frame.app;

import android.app.Activity;
import android.content.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * baijianghai
 * 应用程序Activity管理类：管理Activity和应用程序退出
 */

public class AppManager {

    public static Stack<Activity> activityStack;
    public static AppManager instance;

    public AppManager() {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
    }

    /**
     * 获取单例
     */
    public static AppManager getInstance() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    /**
     * 添加Activity到栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取顶层Activity
     */
    public Activity currentActivity() {
        if (getActivityCount() != 0) {
            return activityStack.lastElement();
        }
        return null;
    }

    /**
     * 获取顶层ActivityClass
     */
    public Class<?> currentActivityClass() {
        if (getActivityCount() != 0) {
            return activityStack.lastElement().getClass();
        }
        return null;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        if (getActivityCount() != 0) {
            Activity activity = activityStack.lastElement();
            if (activity != null) {
                activity.finish();
                activity = null;
            }
        }
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (getActivityCount() != 0 && activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        if (activityStack == null) {
            return;
        }
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                this.finishActivity(activity);
            }
        }
    }

    /**
     * 结束掉指定的List 中的activity
     */
    public void finishListActivity(List<Class<?>> cls) {
        if (activityStack == null) {
            return;
        }
        int stacks = activityStack.size();
        for (int j = 0; j < stacks; j++) {
            int clslength = cls.size();
            for (int i = 0; i < clslength; i++) {
                if (activityStack.get(j).getClass().equals(cls.get(i))) {
                    activityStack.get(j).finish();
                }
            }
        }
    }

    /**
     * 除首页其他全部清楚
     */
    public void finishNonActivity(Class<?> cls) {
        if (activityStack == null) {
            return;
        }
        List<Class<?>> tempActivities = new ArrayList<Class<?>>();
        for (Activity activity : activityStack) {
            if (!activity.getClass().equals(cls)) {
                tempActivities.add(activity.getClass());
            }
        }
        finishListActivity(tempActivities);
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (activityStack == null) {
            return;
        }
        int size = activityStack.size();
        for (int i = 0; i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    public int getActivityCount() {
        return activityStack != null ? activityStack.size() : 0;
    }

    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            this.finishAllActivity();
            // ActivityManager activityMgr =
            // (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
            // activityMgr.restartPackage(context.getPackageName());
            // 同时关闭后台服务
            // System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
